package com.ambulance.project.manager.service.impl;

import com.ambulance.common.exception.CustomException;
import com.ambulance.common.utils.SecurityUtils;
import com.ambulance.project.biz.dto.pad.TreatDoctorInfo;
import com.ambulance.project.biz.entity.Hospital;
import com.ambulance.project.manager.domain.BizDepart;
import com.ambulance.project.manager.domain.BizDoctor;
import com.ambulance.project.manager.mapper.BizDepartMapper;
import com.ambulance.project.manager.mapper.BizDoctorMapper;
import com.ambulance.project.manager.service.IBizDoctorService;
import com.ambulance.project.manager.utils.HelperCommonUtil;
import com.ambulance.project.manager.utils.MgtCommonUtil;
import com.ambulance.project.manager.utils.MgtUserLoginResult;
import com.ambulance.utils.CommonUtils;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 医生管理Service业务层处理
 *
 * @author zr
 * @date 2020-05-11
 */
@Service
public class BizDoctorServiceImpl implements IBizDoctorService {
    @Autowired
    private BizDoctorMapper bizDoctorMapper;
    @Autowired
    private BizDepartMapper bizDepartMapper;
    @Autowired
    private MgtCommonUtil mgtCommonUtil;
    @Autowired
    private HelperCommonUtil helperCommonUtil;

    /**
     * 查询医生管理
     *
     * @param doctorID 医生管理ID
     * @return 医生管理
     */
    @Override
    public BizDoctor selectBizDoctorById(Long doctorID) {
        return bizDoctorMapper.selectBizDoctorById(doctorID);
    }

    /**
     * 查询医生管理列表
     *
     * @param bizDoctor 医生管理
     * @return 医生管理
     */
    @Override
    public List<BizDoctor> selectBizDoctorList(BizDoctor bizDoctor) {
//        bizDoctor.setIsMaster(null);
        final MgtUserLoginResult mgtUserLoginResult = mgtCommonUtil.getMgtUserLoginResult();
        bizDoctor.setUid(new Long(mgtUserLoginResult.getUid()));
        final List<BizDoctor> bizDoctors = bizDoctorMapper.selectBizDoctorList(bizDoctor);
        if (!CommonUtils.checkNull(bizDoctor.getDoctorID())) {
            final BizDoctor bizDoctorById = selectBizDoctorById(bizDoctor.getDoctorID());
            bizDoctors.add(bizDoctorById);
        }
        return bizDoctors;
    }

    public List<BizDoctor> selectBizDoctorListByHelper(BizDoctor bizDoctor) {

        Hospital loginResult = helperCommonUtil.getMgtUserLoginResult();
        bizDoctor.setUid(new Long(loginResult.getUid()));
        final List<BizDoctor> bizDoctors = bizDoctorMapper.selectBizDoctorList(bizDoctor);
        if (!CommonUtils.checkNull(bizDoctor.getDoctorID())) {
            final BizDoctor bizDoctorById = selectBizDoctorById(bizDoctor.getDoctorID());
            bizDoctors.add(bizDoctorById);
        }
        return bizDoctors;
    }


    @Override
    public List<TreatDoctorInfo> selectBizDoctorList2(BizDoctor bizDoctor) {

        List<BizDoctor> doctors = this.selectBizDoctorList(bizDoctor);
        return dealBizDoctor2Info(doctors);
    }

    private List<TreatDoctorInfo> dealBizDoctor2Info(List<BizDoctor> doctors) {
        if (!doctors.isEmpty()) {
            List<TreatDoctorInfo> list = Lists.newArrayList();
            for (BizDoctor item : doctors) {
                TreatDoctorInfo doctor = TreatDoctorInfo.getBeanFromDoctor(item);
                list.add(doctor);
            }
            return list;
        }
        return null;
    }

    @Override
    public List<TreatDoctorInfo> selectBizDoctorListOnHelper(BizDoctor bizDoctor) {
        List<BizDoctor> doctors = selectBizDoctorListByHelper(bizDoctor);
        return dealBizDoctor2Info(doctors);
    }

    /**
     * 新增医生管理
     *
     * @param bizDoctor 医生管理
     * @return 结果
     */

    @Override
    public int insertBizDoctor(BizDoctor bizDoctor) {
        System.out.println(bizDoctor);
        final MgtUserLoginResult mgtUserLoginResult = mgtCommonUtil.getMgtUserLoginResult();
        bizDoctor.setUid(new Long(mgtUserLoginResult.getUid()));
        bizDoctor.setHospital(mgtUserLoginResult.getHospitalName());
        final BizDepart bizDepart = bizDepartMapper.selectBizDepartById(new Long(bizDoctor.getDepartId()));
        bizDoctor.setDepartId(bizDepart.getId().intValue());
        bizDoctor.setDepart(bizDepart.getDepart());
        bizDoctor.setPassword(SecurityUtils.encryptPassword("123456"));
        return bizDoctorMapper.insertBizDoctor(bizDoctor);
    }

    /**
     * 修改医生管理
     *
     * @param bizDoctor 医生管理
     * @return 结果
     */
    @Override
    public int updateBizDoctor(BizDoctor bizDoctor) {
        final BizDepart bizDepart = bizDepartMapper.selectBizDepartById(new Long(bizDoctor.getDepartId()));
        bizDoctor.setDepartId(bizDepart.getId().intValue());
        bizDoctor.setDepart(bizDepart.getDepart());
        return bizDoctorMapper.updateBizDoctor(bizDoctor);
    }

    /**
     * 批量删除医生管理
     *
     * @param doctorIDs 需要删除的医生管理ID
     * @return 结果
     */
    @Override
    public int deleteBizDoctorByIds(Long[] doctorIDs) {
        for (Long item : doctorIDs) {
            final BizDoctor bizDoctor = selectBizDoctorById(item);
            if (bizDoctor.getClassStatus().equals(1)) {
                throw new CustomException("您尚在班组中,无法删除");
            }
        }
        return bizDoctorMapper.deleteBizDoctorByIds(doctorIDs);
    }

    /**
     * 删除医生管理信息
     *
     * @param doctorID 医生管理ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteBizDoctorById(Long doctorID) {
        final BizDoctor bizDoctor = selectBizDoctorById(doctorID);
        if (bizDoctor.getClassStatus().equals(1)) {
            throw new CustomException("您尚在班组中,无法删除");
        }
        return bizDoctorMapper.deleteBizDoctorById(doctorID);
    }

    /**
     * 冻结医生
     *
     * @param doctorID 医生管理ID
     * @return
     */
    @Override
    public int lightBizDoctor(Long doctorID) {
        final BizDoctor bizDoctor = bizDoctorMapper.selectBizDoctorById(doctorID);
        if (bizDoctor.getAccountStatus().equals(1) && bizDoctor.getStatus().equals(1)) {
            throw new CustomException("您的账号已冻结,请勿重复冻结");
        }
        return bizDoctorMapper.lightBizDoctorById(doctorID);
    }
}
